package com.yuan.order.service.impl;

import com.yuan.order.dataobject.ProductInfo;
import com.yuan.order.dto.CartDTO;
import com.yuan.order.enums.ProductStatusEnum;
import com.yuan.order.enums.ResultEnum;
import com.yuan.order.execption.SellException;
import com.yuan.order.repository.ProductInfoRepository;
import com.yuan.order.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.NoSuchElementException;

@Service
public class ProductInfoServiceImpl implements ProductInfoService {
    @Autowired
    private ProductInfoRepository repository;

    /**
     * 查找一件商品信息
     *
     * @param productId
     * @return
     */
    @Override
    public ProductInfo findById(String productId) {
        return repository.findById(productId).get();
    }

    /**
     * 查询所有在架商品列表
     *
     * @return
     */
    @Override
    public List<ProductInfo> findUpAll() {
        return repository.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    /**
     * 查找所有商品分页显示
     * @param pageable
     * @return
     */
    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        //PageRequest request = PageRequest.of(0, 10);
        return repository.findAll(pageable);
    }

    /**
     * 保存一个商品信息
     *
     * @param productInfo
     * @return
     */
    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return repository.save(productInfo);
    }

    /**
     * 加库存
     *
     * @param cartDTOList
     * @return
     */
    @Override
    @Transactional(rollbackOn = Exception.class)
    public void increaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO : cartDTOList) {
            ProductInfo productInfo = findById(cartDTO.getProductId());
            if (productInfo == null) {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer result = productInfo.getProductStock() + cartDTO.getProductQuantity();
            productInfo.setProductStock(result);
            repository.save(productInfo);
        }
    }

    /**
     * 减库存
     *
     * @param cartDTOList
     * @return
     */
    @Override
    @Transactional(rollbackOn = Exception.class)

    public void decreaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO : cartDTOList) {
            ProductInfo productInfo = findById(cartDTO.getProductId());
            if (productInfo == null) {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer result = productInfo.getProductStock() - cartDTO.getProductQuantity();
            if (result < 0) {
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            }
            productInfo.setProductStock(result);
            repository.save(productInfo);
        }
    }

    /**
     * 下架
     *
     * @param productId
     * @return
     */
    @Override
    public ProductInfo offSale(String productId) {
        ProductInfo productInfo = null;
        try {
            productInfo = repository.findById(productId).get();

        } catch (NoSuchElementException e) {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        //判断商品状态
        if (productInfo.getProductStatusEnum() == ProductStatusEnum.DOWN) {
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }
        //更新状态
        productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
        return repository.save(productInfo);
    }

    /**
     * 上架
     *
     * @param productId
     * @return
     */
    @Override
    public ProductInfo onSale(String productId) {
        ProductInfo productInfo = null;
        try {
            productInfo = repository.findById(productId).get();

        } catch (NoSuchElementException e) {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        //判断商品状态
        if (productInfo.getProductStatusEnum() == ProductStatusEnum.UP) {
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }
        //更新状态
        productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
        return repository.save(productInfo);
    }
}
